<?php

class Distribusi extends CI_Controller {
	
	function Distribusi()
	{
		parent::__construct();
		$this->load->model('Home_model', '', TRUE);
		$this->load->model('auth_model', '', TRUE);
		$this->load->model('distribusi_model', '', TRUE);
		$this->load->model('sapi_model', '', TRUE);
		$this->load->model('peternakan_model', '', TRUE);
		$this->load->model('titik_penjualan_model', '', TRUE);
		$this->load->model('rph_model', '', TRUE);
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE)
		{
			redirect('auth');
		}
		
		
		
		// Load default view
		$data['title'] = "Distribusi";
		$data['main_view'] = 'distribusi/distribusi_view';
		
		$id_role = $this->session->userdata('id_role'); 
		$data['hak_akses0'] = $this->Home_model->get_hak_akses($id_role,0);
		$data['hak_akses1'] = $this->Home_model->get_hak_akses($id_role,1);
		$data['hak_akses2'] = $this->Home_model->get_hak_akses($id_role,2);
		
		$jenis_sapi = $this->sapi_model->get_jenis_sapi();
		if($jenis_sapi == null)
			$data['all_jenis_sapi'] = null;
		else
			$data['all_jenis_sapi'] = $jenis_sapi->result_array();
		
		// Load view
		$this->load->view('template', $data);
	}
	
	function insert_to_distribusi(){
		$revisi = $this->input->post('revisi');
		$id_dari = $this->input->post('dari');
		
		
		$arr = array(
			'DARI' => $this->input->post('dari'),
			'KE' => $this->input->post('ke'),
			'ID_SAPI' => $this->input->post('id_sapi'),
			
			'JUMLAH' => $this->input->post('jumlah'),
			
			'KOMENTAR' => $this->input->post('komentar')
		);
		
		//var_dump($arr);
		$id_distribusi = $this->distribusi_model->insert_to_distribusi($arr);
		
		$arr2 = array(
			'ID_DISTRIBUSI' => $id_distribusi,
			'ID_USER' => $this->session->userdata('id_user'),
			'REVISI' => $revisi
		);
		
		$this->distribusi_model->insert_to_perubahan_status($arr2);
		
		$arr3 = array(
			'ID_USER' => $this->session->userdata('id_user'),
			'ID_SAPI' => $this->input->post('id_sapi'),
			'ID_TITIK_PASOK' => $this->input->post('dari'),
			'JUMLAH' => $this->input->post('jumlah'),
			'REVISI' => 0,
			'ALASAN_PERUBAHAN' => 5,
			'KOMENTAR' => $this->input->post('komentar')
		);
		
		$this->distribusi_model->insert_to_stok($arr3);
		
		$arr4 = array(
			'ID_USER' => $this->session->userdata('id_user'),
			'ID_SAPI' => $this->input->post('id_sapi'),
			'ID_TITIK_PASOK' => $this->input->post('ke'),
			'JUMLAH' => $this->input->post('jumlah'),
			'REVISI' => 0,
			'ALASAN_PERUBAHAN' => 4,
			'KOMENTAR' => $this->input->post('komentar')
		);
		
		$this->distribusi_model->insert_to_stok($arr4);
	}
	
	function auto_complete_titik_pasok(){
		$term = trim(strip_tags($_GET['term']));
		
		$matches = array();
		$titik_pasok = $this->distribusi_model->get_all_titik_pasok();
		
		foreach($titik_pasok->result_array() as $sd){
			if(stripos($sd['NAMA'], $term) !== false){
				// Add the necessary "value" and "label" fields and append to result set
				$sd['value'] = $sd['NAMA'];
				$sd['label'] = "{$sd['NAMA_KOTA']}, {$sd['NAMA']}";
				$matches[] = $sd;
			}
		}
		 
		// Truncate, encode and return the results
		$matches = array_slice($matches, 0, 10);
		echo json_encode($matches);
	}
	
	function auto_complete_sapi(){
		$term = trim(strip_tags($_GET['term']));
		
		$matches = array();
		$daging_sapi = $this->sapi_model->get_jenis_sapi();
		
		foreach($daging_sapi->result_array() as $sd){
			if(stripos($sd['NAMA'], $term) !== false){
				// Add the necessary "value" and "label" fields and append to result set
				$sd['value'] = $sd['NAMA'];
				$sd['label'] = "jenis sapi, {$sd['NAMA']}";
				$matches[] = $sd;
			}
		}
		
		$matches = array_slice($matches, 0, 5);
		
		$daging_sapi = $this->sapi_model->get_daging_sapi();
		
		foreach($daging_sapi->result_array() as $sd){
			if(stripos($sd['NAMA'], $term) !== false){
				// Add the necessary "value" and "label" fields and append to result set
				$sd['value'] = $sd['NAMA'];
				$sd['label'] = "jenis daging, {$sd['NAMA']}";
				$matches[] = $sd;
			}
		}
		 
		// Truncate, encode and return the results
		$matches = array_slice($matches, 0, 10);
		echo json_encode($matches);
	}
	
}